package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.PProduct;
import com.mk.cloud.common.domain.PScheme;
import com.mk.cloud.common.enums.ErrorCode;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.*;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("scheme")
public class PSchemeController extends CookieService {

    @Inject
    private PScheme.Mapper pSchemeMapper;


    @Description("方案--方案列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PScheme> pProductList(
            @Description("筛选条件-姓名") String sname,
            @Description("筛选条件-发布时间开始时间") String publishtimeb,
            @Description("筛选条件-发布时间结束时间") String publishtimee,
            @Description("筛选条件-品牌id") String brid,
            @Description("筛选条件-方案状态") String publishstatus,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PScheme> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        if(S.isNotBlank(publishtimeb)){
            publishtimeb = publishtimeb+" 00:00:00";
        }
        if(S.isNotBlank(publishtimee)){
            publishtimee = publishtimee+" 23:59:59";
        }
        map.put("sname", sname);
        map.put("publishtimeb", publishtimeb);
        map.put("publishtimee", publishtimee);
        map.put("brid", brid);
        map.put("publishstatus", publishstatus);
        map.put("coid", me.getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pSchemeMapper.getSQLManager().pageQuery("pScheme.select_scheme_page_list", PScheme.class, query);
        return query;
    }


    @Description("方案--方案详情")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PScheme getPScheme(@Description("方案id") String sid) {
        if (S.isEmpty(sid)) {
            return new PScheme();
        }
        return pSchemeMapper.single(sid);
    }

    @Description("方案--方案保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePScheme(@Description("方案对象") PScheme scheme) {
        scheme.setCoid(me.getCoid());
        if (S.eq(scheme.getPublishstatus(), GlobalValues.PUBLISH_STATUS_1.value())) {
            scheme.setPublishtime(DateUtils.getTimeNow());
            scheme.setPublishuserid(me.getUserid());
        }
        if (S.isNotBlank(scheme.getSid())) {
            pSchemeMapper.updateTemplateById(scheme);
            return scheme.getSid();
        } else {
            String sid = S2.UUID();
            scheme.setSid(sid);
            scheme.setCreatetime(DateUtils.getTimeNow());
            pSchemeMapper.insert(scheme);
            return sid;
        }
    }

    @Description("方案--方案状态更新")
    @With({ICLogin.class, ITransacte.class})
    @PutAction("updatePublishstatus.json")
    public String updatePublishstatus(@Description("方案id") String sid, @Description("方案状态")String publishstatus) {
        PScheme scheme = new PScheme();
        scheme.setSid(sid);
        scheme.setPublishstatus(publishstatus);
        if (S.eq(publishstatus, GlobalValues.PUBLISH_STATUS_1.value())) {
            scheme.setPublishtime(DateUtils.getTimeNow());
            scheme.setPublishuserid(me.getUserid());
        }
        pSchemeMapper.updateTemplateById(scheme);
        return sid;
    }

}
